$(function(){
	$("#page-content").on("pageshow.0207", function () {
		var dd = $('.dd').nestable();
		dd.nestable("collapseAll");
		//删除已存在的人员
		$(document).on("click",".fa-trash-o.exists-user",function(){
			var $this = $(this);
			$.request($.router.fxd.removeUser,{
				data:{
				    userId:$this.data("user"),
				    deptId:$this.data("dept"),
				    fxid:$this.data("fxid")
				},
				callback:function(json){
					$this.parents("li:first").remove();
				}
			});
		});
		//删除已存在部门
		$(document).on("click",".fa-trash-o.exists-dept",function(){
			var $this = $(this);
			$.request($.router.fxd.removeDept,{
				data:{
				    deptId:$this.data("dept"),
				    fxid:$this.data("fxid")
				},
				callback:function(json){
					$this.parents("li:first").remove();
				}
			});
		});
		 
		var $this = $(this);
		$(".fa-plus",$this).click(function(){
			var $this = $(this);
			var fxid = $this.data("fxid");
			var $fxid = $("#"+fxid);
			
			var html = [
				'<li class="dd-item dd2-item" data-id="" data-fxid="'+fxid+'" data-dept="">',
				'    <div class="dd2-handle">',
				'        <i class="normal-icon ace-icon fa  fa-folder-o red bigger-130"></i>',
				'        <i class="drag-icon ace-icon fa fa-arrows bigger-125"></i>',
				'    </div>',
				'    <div class="dd2-content">',
				'		 <input>',
				'        <div class="pull-right action-buttons">',
				'            <a class="blue" href="#">',
				'                <i class="ace-icon fa fa-floppy-o bigger-130"></i>',
				'            </a>',
				'        </div>',
				'        <div class="pull-right action-buttons">',
				'            <a class="red" href="#">',
				'                <i class="ace-icon fa fa-trash-o bigger-130" data-fxid="'+fxid+'" data-dept=""></i>',
				'            </a>',
				'        </div>',
				'    </div>',
				'    <ol class="user-list">',
				'',
				'    </ol>',
				'</li>'
			].join("");

			var $html = $(html);
			
			var $ol = $fxid.children("ol");
			if($ol.length==0){
				$fxid.append('<ol class="dd-list"></ol>');
				$ol = $fxid.children("ol");
			}
			$ol.append($html);
			
			$html.find(".fa-floppy-o").click(function(){
				var $this = $(this);
				var deptid = $html.find("input").val();
				$.request($.router.fxd.saveDept,{
					data:{
					    deptId:deptid,
					    fxid:fxid
					},
					callback:function(json){
						$html.find("input").replaceWith(json.name);
						var $trash = $html.find(".fa-trash-o");
						$trash.attr("data-dept",deptid);
						$trash.addClass("exists-dept");
						$this.remove();
					}
				});
			});
		});
		
		$(".fa-user",$this).click(function(){
			var $u = $(this);
			var $p = $u.parents(".dd-item:first");
			var $ol = $p.find(".user-list");
			
			var html = [
				'<li class="dd-item">',
					'<div class="dd2-content">',
						'工号:<input />',
						'<div class="pull-right action-buttons">',
							'<a class="blue" href="#">',
								'<i class="ace-icon fa fa-floppy-o bigger-130"></i>',
							'</a>',
						'</div>',
						'<div class="pull-right action-buttons">',
							'<a class="red" href="#">',
								'<i class="ace-icon fa fa-trash-o bigger-130"></i>',
							'</a>',
						'</div>',
					'</div>',
				'</li>'
			].join("");
			var $li = $(html);
			$ol.append($li);
			
			$li.find(".fa-trash-o").click(function(){
				var $s = $(this);
				$s.parents("li:first").remove();
			});
			$li.find(".fa-floppy-o").click(function(){
				var $this = $(this);
				var uid = $li.find("input").val();
				$.request($.router.fxd.saveUser,{
					data:{
					    userId:uid,
					    deptId:$p.data("dept"),
					    fxid:$p.data("fxid")
					},
					callback:function(json){
						$li.find("input").replaceWith(json.name);
						var $trash = $li.find(".fa-trash-o");
						$trash.attr("data-dept",$p.data("dept"));
						$trash.attr("data-fxid",$p.data("fxid"));
						$trash.attr("data-user",uid);
						$trash.addClass("exists-user");
						$this.remove();
					}
				});
			});
			//dd.expandItem($u.parents(".dd-item:first"));
		});
    });
});